package EGS.impl;
import java.util.concurrent.ExecutorService;

import javax.swing.JTable;

import org.dom4j.Document;
import org.dom4j.Element;

import TOOLS.SQLtool;
import TOOLS.errCodeManagement;
import WORKPLACE.stationClient;

public class EGS_execute_service5801Impl{
	public boolean isUnknowCase = false;
	ExecutorService iExecutorService;
	Document MSG;
	boolean isPanel = false;
	stationClient iClient=null;
	SQLtool sql = null;
	/**
	 * 5801 获取工单分类
	 */
	public EGS_execute_service5801Impl(stationClient nClient, Document MSG_in, ExecutorService executorService, 
			SQLtool goldenSql) throws Exception {
		super();
		sql=goldenSql;
		if(nClient!=null) {
			iClient=nClient;
		}else {
			iClient=null;
		}
		MSG = MSG_in;
		iExecutorService = executorService;
	}

	public Document runCal() {
		String msgClass = "";
		try {
			if (MSG == null) {
				return null;
			}
			Element Mroot = MSG.getRootElement();
			msgClass = Mroot.element("header").attribute("messageClass").getValue();
			String iStation;

			iStation = Mroot.element("header").element("location").attribute("equipmentName").getValue();

			if (msgClass.equals("5801")) {// 试运行阶段二，用580的时机来切换工单到对应工位。
				String lv2Key = "orderInfo";
				String orderNo = null;
				orderNo = Mroot.element("body").element(lv2Key).attribute("orderNumber").getValue();
				if (orderNo == null) {
					orderNo = "";
				}
				Mroot.remove(Mroot.element("body"));
				Element eleB = Mroot.addElement("body");
				Element eleR = eleB.addElement("result");

				if(orderNo==null || orderNo.length()<1) {
					String str = "工单号获取失败";
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", "0");
					AKSLogs(58010, "" + orderNo + "_" +iStation+"_"+str );
				}else {
					String str = "SELECT orderNumber,masterOrderNumber \r\n" + 
							"FROM [dbo].[order_list] \r\n" + 
							"where orderNumber='"+orderNo+"'  ";
					JTable t = sql.select(str);
					int orderType=-1;
					if (t != null && t.getRowCount() > 0) {
						String o1=""+t.getValueAt(0, 0);
						String o2=""+t.getValueAt(0, 1);
						if(o1.equals(o2)) {
							orderType=1;//总成工单
						}else {
							orderType=2;//部件工单
						}
					}else {
						str = "SELECT *\r\n" + 
								"FROM [dbo].[order_list_panel] \r\n" + 
								"where panelOrderNumber='"+orderNo+"'  ";
						t = sql.select(str);
						if (t != null && t.getRowCount() > 0) {
							orderType=3;//拼板工单
						}
					}
					if(orderType==-1) {
						eleR.addAttribute("errorCode", "1");
						eleR.addAttribute("errorText", orderNo+"工单号不存在");
						eleR.addAttribute("actionCode", "0");
					}else {
						eleR.addAttribute("errorCode", "0");
						eleR.addAttribute("errorText", "Valid");
						eleR.addAttribute("actionCode", "0");

						Element eleO = eleB.addElement("orderInfo");
						eleO.addAttribute("orderType", ""+orderType);
					}
					
						
					
				}
				
			} else {
				isUnknowCase = true;
				return null;
			}
			return MSG;
		} catch (Exception e) {
			if(iClient==null) {
				Element Mroot = MSG.getRootElement();
				Element eleR = Mroot.element("body").addElement("result");
				eleR.addAttribute("errorCode", "1");
				eleR.addAttribute("errorText", "工位信息获取失败");
				eleR.addAttribute("actionCode", "0");
				return MSG;
			}
			String str = errCodeManagement.getErrText(e.getMessage());
			System.out.println(str);
			return null;
		}

	}

	public void AKSLogs(int iType, String ilogClientNews) {
		SQLtool.AKSLogs(iType, ilogClientNews);
	}

}
